/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank.controllers;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import tank.models.DirectionInfo;
import tank.models.GameSettings;
import tank.models.Global;
import tank.models.Turret;
import tank.views.TankPlayView;

/**
 *
 * @author shengchang
 */
public class TankPlayController implements KeyListener, MouseListener, MouseMotionListener{
    private TankPlayView view;
    private JFrame frame;
    
    public TankPlayView getView()
    {
        return this.view;
    }
    
    public void setView( TankPlayView view )
    {
        this.view = view;
    }
    
    public JFrame getFrame()
    {
        return this.frame;
    }
    
    public void setFrame(JFrame frame)
    {
        this.frame = frame;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if( e.getKeyCode() == KeyEvent.VK_UP )
        {
            if(Global.myTank.canMove(0))
            {
                Global.myTank.setY(Global.myTank.getY() - Global.myTank.getSpeed());
            }
            Global.myTank.setDirection(0);
            //view.repaint();
        }else if( e.getKeyCode() == KeyEvent.VK_DOWN )
        {
            if(Global.myTank.canMove(2))
            {
                Global.myTank.setY(Global.myTank.getY() + Global.myTank.getSpeed());
            }
            Global.myTank.setDirection(2);
            Global.myTank.setCanMove(true);
            //view.repaint();
        }else if( e.getKeyCode() == KeyEvent.VK_LEFT )
        {
            if(Global.myTank.canMove(3))
            {
                Global.myTank.setX(Global.myTank.getX() - Global.myTank.getSpeed());
            }
            Global.myTank.setDirection(3);
        }else if( e.getKeyCode() == KeyEvent.VK_RIGHT )
        {
            if(Global.myTank.canMove(1))
            {
                Global.myTank.setX(Global.myTank.getX() + Global.myTank.getSpeed());
            }
            Global.myTank.setDirection(1);
        }else if(e.getKeyCode() == KeyEvent.VK_F)
        {
            if( Global.myTank.getBulletVector().size() < 10 )
            {
                Global.myTank.shotBullet();
                new SoundPlayer(GameSettings.shotAudio).start();
            }
        }else if(e.getKeyCode() == KeyEvent.VK_R)
        {
            Global.myTank.setDirection(Global.myTank.getDirection()+Global.myTank.getRotateSpeed());
        }else if(e.getKeyCode() == KeyEvent.VK_T)
        {
            Global.myTank.getTurret().setDirection(Global.myTank.getTurret().getDirection() + Global.myTank.getTurret().getRotateSpeed());
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if( Global.myTank.getBulletVector().size() < 10 )
        {
            Global.myTank.shotBullet();
            new SoundPlayer(GameSettings.shotAudio).start();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
       
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        //set direction of the turret     
        if( e.getX() - GameSettings.viewStartX > (Global.myTank.getX() + 25 ) )
            Global.myTank.getTurret().getDirectionInfo().setDirectionX(1);
        else
            Global.myTank.getTurret().getDirectionInfo().setDirectionX(-1);

        if( e.getY() - GameSettings.viewStartY > (Global.myTank.getY() + 25 ) )
            Global.myTank.getTurret().getDirectionInfo().setDirectionY(1);
        else
            Global.myTank.getTurret().getDirectionInfo().setDirectionY(-1);    
        
        //slope
        if( e.getX() - GameSettings.viewStartX != (Global.myTank.getX() + 25 ) )
        {
            double slope = (double)Math.abs(e.getY()- GameSettings.viewStartY - (Global.myTank.getY() + 25 ))/(double)Math.abs(e.getX() - GameSettings.viewStartX - (Global.myTank.getX() + 25 ));
            Global.myTank.getTurret().getDirectionInfo().setSlope(slope);
        }
        else
        {
            Global.myTank.getTurret().getDirectionInfo().setSlope(-1.0);
        }
    }
}
